<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2012 HongPing All rights reserved.
// +----------------------------------------------------------------------
// | ActionName:仓库组装单模块
// +----------------------------------------------------------------------
// | Author: HongPing <hongping626@qq.com>
// +----------------------------------------------------------------------

class AssemblyAction extends CommonAction {
	function _filter(&$map){
        //$map['id'] = array('egt',2);
		//$map['wahname|egname|wahcode|wahaddr'] = array('like',"%".$_POST['selectname']."%");
        
	}
   //页面加载前生产单据编号
    function _before_index(){
        $newCode = $this->getSerialNum('ZZ','Ordermain');
        $this->assign ( 'newCode', $newCode );
        $this->assign ( 'dtime', date('Y-m-d') );
    }
    
    function addOrders(){
        if($_POST['ordernumber']){$mainData['ordernumber'] = $_POST['ordernumber'];}else{$this->error('单据编号不能为空。');exit;}//單據編號
        if($_POST['ordertime']){$mainData['ordertime'] = strtotime($_POST['ordertime']);}else{$this->error('单据日期不能为空。');exit;}//單據日期        
        if($_POST['orderUser_id']){$mainData['orderuser'] = $_POST['orderUser_id'];}else{$this->error('经办人不能为空。');exit;}//经办人
        if($_POST['warehouse_id']){$mainData['warehouse'] = $_POST['warehouse_id'];}else{$this->error('接收仓库不能为空。');exit;}//接收仓库        
        $mainData['remarks'] = $_POST['remarks'];//摘要
        if($_POST['total_nums1']){$mainData['ordernums'] = $_POST['total_nums1'];}else{$this->error('入库商品数量为0。');exit;}//入库數量
        if($_POST['total_nums2']){$mainData['outnums'] = $_POST['total_nums2'];}else{$this->error('出库商品数量为0。');exit;}//出库數量
        $mainData['orderamount'] = $_POST['total_amount1'];//入库金额
        $mainData['outamount'] = $_POST['total_amount2'];//出库金额
        $mainData['createuser'] = $_POST['createManid'];//制单人
        $mainData['ordertype'] = 5;//设置为组装单
        $mainData['status'] = 0;//设置为未生效
        $mainData['dealstatus'] = 0;//设置为未结
        $mainData['createtime'] = strtotime(date('Y-m-d H:i:s'));//制单时间
        $mainData['updatetime'] = strtotime(date('Y-m-d H:i:s'));//更新时间
        //$a = $items['orgNumber'];
        
        //入库
        $aProidData = $_POST['items1_proid'];
        $countRow = count($aProidData);
        if($countRow==0){$this->error('单据没有添加任何入库商品信息。');exit;}
        $aPNumbData = $_POST['items1_partnumber'];
        $aPnameData = $_POST['items1_prodname'];
        $aCountData = $_POST['items1_orgCount'];
        $aPriceData = $_POST['items1_orgPrice'];
        $aDisctData = $_POST['items1_orgDiscount'];
        $aTotalData = $_POST['items1_orgTotal'];
        $aMarksData = $_POST['items1_orgMark'];
        
        //出库
        $aProidData2 = $_POST['items2_proid'];
        $countRow2 = count($aProidData2);
        if($countRow2==0){$this->error('单据没有添加任何出库商品信息。');exit;}
        $aPNumbData2 = $_POST['items2_partnumber'];
        $aPnameData2 = $_POST['items2_prodname'];
        $aCountData2 = $_POST['items2_orgCount'];
        $aPriceData2 = $_POST['items2_orgPrice'];
        $aDisctData2 = $_POST['items2_orgDiscount'];
        $aTotalData2 = $_POST['items2_orgTotal'];
        $aMarksData2 = $_POST['items2_orgMark'];
                
        
        //初始化数据字典      
        $ordermainModel = D('Ordermain');
        $detailModel = D('Orderdetails'); 
        $stockModel = D('Stocks');
        //设置操作单据条件
        $map['ordernumber'] = $_POST['ordernumber'];
                
        //验证出库库存
        $stockMap['prodid'] = array('in',$aProidData2);//查找商品对于的库存
        $stockMap['warehid'] = $mainData['warehouse'];//查找对应的仓库库存        
        $outNums = $mainData['outnums'];
        if($_POST['goStock']==1){          
          $stockNums = $stockModel->where($stockMap)->sum('stocknums');
          if(bccomp($stockNums,$outNums)<0){$this->error('当前库存量不足,无法结算出库数量');exit;}  
        }
        
        //依次检查单据商品项目库存是否满足
        for($i=0;$i<$countRow2;$i++){
            $orderNum = 0;
            $stockNum = 0;
            unset($stockMap);
            $proName = '';
            $orderNum =  $aCountData2[$i];//商品对应单据数量
            $stockMap['prodid'] =  $aProidData2[$i];//商品条件
            $stockMap['warehid'] = $mainData['warehouse'];//仓库条件
            $stockNum = $stockModel->where($stockMap)->sum('stocknums');//对应商品库存            
            if(bccomp($stockNum,$orderNum)<0){$this->error('发现['.$aPnameData2[$i].']在选中仓库库存量不足,退出操作。');exit;}
        }
                      
        if(($ordermainModel->where($map)->count('id'))>0){
            $this->error('该单据已提交过,请勿重复提交单据。');exit;
        }
        //以上条件满足开始数据录入
        if($ordermainModel->add($mainData)){                 
                //循环本单据每笔单据信息写入数据库
                //入库
                for($i=0;$i<$countRow;$i++){
                    $sucsCount = 0;
                    unset($detailData);
                    unset($stkData);
                    $detailData['ordersort']   = $i+1;
                    $detailData['ordernumber'] = $_POST['ordernumber'];
                    $detailData['prodid']      = $aProidData[$i];
                    $detailData['prodcode']    = $aPNumbData[$i];
                    $detailData['ordernums']   = $aCountData[$i];
                    $detailData['orderprice']  = $aPriceData[$i];
                    $detailData['discounts']   = $aDisctData[$i];
                    $detailData['totalamount'] = $aTotalData[$i];
                    $detailData['remarks']     = $aMarksData[$i];
                    $detailData['iotype']     = 1;//1为入库2为出库
                    if($detailModel->add($detailData)){
                        if($_POST['goStock']==1){//如果有选择结库                                
                            $stkMap['prodid'] = $detailData['prodid'];
                            $stkMap['warehid'] = $mainData['warehouse'];
                            $stkCount = $stockModel->where($stkMap)->count('id');
                            if($stkCount>0){//库存商品已有该商品库存
                               $stockModel->where($stkMap)->setInc('stocknums',$detailData['ordernums']);//增加库存量
                               $stockModel->where($stkMap)->setField('updatetime',time());//更新库存时间
                            }else{
                               $stkData['prodid'] = $detailData['prodid'];
                               $stkData['stocknums'] = $detailData['ordernums'];
                               $stkData['warehid'] = $mainData['warehouse'];
                               $stkData['updatetime'] = time();
                               $stockModel->add($stkData);//增加商品库存
                            }
                            
                        }
                    $sucsCount = $sucsCount+1;
                    }
                } 
                //出库
                for($i=0;$i<$countRow2;$i++){
                    $sucsCount2 = 0;
                    unset($detailData2);
                    $detailData2['ordersort']   = $i+1;
                    $detailData2['ordernumber'] = $_POST['ordernumber'];
                    $detailData2['prodid']      = $aProidData2[$i];
                    $detailData2['prodcode']    = $aPNumbData2[$i];
                    $detailData2['ordernums']   = $aCountData2[$i];
                    $detailData2['orderprice']  = $aPriceData2[$i];
                    $detailData2['discounts']   = $aDisctData2[$i];
                    $detailData2['totalamount'] = $aTotalData2[$i];
                    $detailData2['remarks']     = $aMarksData2[$i];
                    $detailData2['iotype']     = 2;
                    if($detailModel->add($detailData2)){
                        if($_POST['goStock']==1){//如果有选择结库                                
                            $stkMap['prodid'] = $detailData['prodid'];
                            $stkMap['warehid'] = $mainData['warehouse'];
                            $stkCount = $stockModel->where($stkMap)->count('id');
                            if($stkCount>0){//库存商品已有该商品库存
                               $stockModel->where($stkMap)->setDec('stocknums',$detailData['ordernums']);//减少库存量
                               $stockModel->where($stkMap)->setField('updatetime',time());//更新库存时间
                            }//else{
                               //$stkData['prodid'] = $detailData['prodid'];
                               //$stkData['stocknums'] = $detailData['ordernums'];
                               //$stkData['warehid'] = $mainData['warehouse'];
                               //$stkData['updatetime'] = time();
                               //$stockModel->add($stkData);//增加商品库存
                            //}
                            
                        }
                    $sucsCount2 = $sucsCount2+1;
                    }
                }                    
            if($sucsCount = $countRow and $sucsCount2 = $countRow2){ 
                $this->assign ( "jumpUrl", $this->getReturnUrl());
               $this->success('单据[ '.$_POST['ordernumber'].' ]已提交,请至[业务单据池]处理单据状态。'); 
            }else{
               $ordermainModel->where($map)->delete();
               $detailModel->where($map)->delete();
               $this->error('单据[ '.$_POST['ordernumber'].' ]提交失敗,详细信息写入失败。');                   
            }                
        }else{
            $this->error('单据保存失败。');
        }      
        //$this->success($a);
    }

    
 
}
?>